<?php
require("init.php");
if(isset($_SESSION['user_name'])){
$pg = isset($_REQUEST['pg'])?$_REQUEST['pg']:'1';
$id = isset($_REQUEST['id'])?$_REQUEST['id']:'';
$tijiao = isset($_REQUEST['tijiao'])?$_REQUEST['tijiao']:'';
$resultEdit = array();
if($tijiao==1){
	$title = $_REQUEST['title'];
	$id = $_REQUEST['id'];
	$content = $_REQUEST['content'];
	$title = addslashes($title);$content = addslashes($content);
	$sql = "update hairArticle set content = '".$content."',title = '".$title."' where id = ".$id."";
	$GLOBALS['db']->dbquery($sql);
}
if(!empty($id)){
	$sql = "select * from hairArticle where id = ".$id."";
	$resultEdit = $GLOBALS['db']->dbGetRow($sql);
}
$pageNum = 10;
$nextPage = $pageNum * ($pg-1);

$sql = "select id,title,flag,timeStamps,url from hairArticle order by id desc limit $nextPage,$pageNum";
$row = $GLOBALS['db']->dbfetchAll($sql);

$sql = "select templateId,count(*) as clickNum from clickTrack group by templateId;";
$rows = $GLOBALS['db']->dbfetchAll($sql);

foreach($row as $key=>$value){
	foreach($rows as $k=>$v){
		if(empty($v['templateId'])){
			$v['templateId'] = 1;
		}
		if($value['id']==$v['templateId']){
			$row[$key]['clickNum']=$v['clickNum'];
		}
	}
	if(!array_key_exists("clickNum",$row)){
		$row[$key]['clickNum'] = 0;
	}
}
?>
<title>模板列表</title>

<table width="1299" border="0">
  <tr>
    <td width="450" height="348" valign="top">
<table width="433" border="0" style="background:#FFFFFF">
  <tbody><tr>
    <td colspan="2">Wish you a Happy New Year!</td>
    </tr>
  <tr>
    <td width="61">ID</td>
    <td width="228">TITLE</td>
    <td width="21">flag</td>
    <td width="40">Edit</td>
    </tr>  
<?php foreach ($row as $key => $value) {?>
  <tr>
    <td><?php echo $value['id'];?></td>
    <td style = "font-size:12px;"><?php echo $value['title'];?></td>
    <td><?php echo $value['flag'];?></td>
    <td style="font-size:12px"><a href="list.php?id=<?php echo $value['id'];?>" target="_blank"></a><a href="list.php?id=<?php echo $value['id'];?>&pg=<?php echo $pg;?>" target="_self">Edit</a></td>
    </tr>
<?php } ?>
  <tr>
    <td colspan="4" style="font-size:12px"><?php
	 $sql = "select count(*) from hairArticle";
	 $totalNum = $GLOBALS['db']->dbGetRow($sql);
	 $page = new Pager($totalNum[0],$pageNum,$style="page");?></td>
    </tr>  
</tbody></table>
	</td>
    <td width="839" valign="top">
	<form action="" name="" method="post">
		<table width="759" border="0">
		  <tr>
			<td width="102">ID:</td>
			<td width="488"><?php if(!empty($resultEdit)){echo $resultEdit['id'];}else{echo "";}?></td>
		  </tr>
		  <tr>
		    <td>URL:</td>
		    <td><a href="<?php if(!empty($resultEdit)){echo $resultEdit['url'];}else{echo "";}?>">Open</a><input name="title" type="text" value="<?php if(!empty($resultEdit)){echo $resultEdit['url'];}else{echo "";}?>" size="80" /></td>
	      </tr>
		  <tr>
			<td>TITLE:</td>
			<td><input name="title" type="text" value="<?php if(!empty($resultEdit)){echo $resultEdit['title'];}else{echo "";}?>" size="80" /></td>
		  </tr>
		  <tr>
			<td height="257">CONTENT:</td>
			<td valign="top"><textarea name="content" cols="105" rows="35"><?php if(!empty($resultEdit)){echo $resultEdit['content'];}else{echo "";}?></textarea></td>
		  </tr>
		  <tr>
			<td>&nbsp;</td>
			<td><input type="hidden" name="id" value="<?php if(!empty($resultEdit)){echo $resultEdit['id'];}else{echo "";}?>" />
			<input type="hidden" name="tijiao" value="1" />
			<input type="submit" name="Submit" value="提交" /></td>
		  </tr>
	  </table>
	</form>
	</td>
  </tr>
</table>


<?php }
else{
	exit;
}
?>
